using PP.Basic;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace PP.PPPoker
{
    /// <summary>
    /// CardDealer 对象
    /// </summary>
    public class CardDealer : SingletonUnsafe<CardDealer>
    {
        private const string Tag = nameof(CardDealer);

        private int _currentIndex; // 当前发牌索引
        private System.Random _rng;


        // Private Variables Begin
        private List<int> _deck = new List<int> {
            0x102, 0x103, 0x104, 0x105, 0x106, 0x107, 0x108, 0x109, 0x10a, 0x10b, 0x10c, 0x10d, 0x10e,
            0x202, 0x203, 0x204, 0x205, 0x206, 0x207, 0x208, 0x209, 0x20a, 0x20b, 0x20c, 0x20d, 0x20e,
            0x302, 0x303, 0x304, 0x305, 0x306, 0x307, 0x308, 0x309, 0x30a, 0x30b, 0x30c, 0x30d, 0x30e,
            0x402, 0x403, 0x404, 0x405, 0x406, 0x407, 0x408, 0x409, 0x40a, 0x40b, 0x40c, 0x40d, 0x40e,
            0x50f, // Joker 小王
            0x610, // Joker 大王
        };

        // Private Variables End

        private void ShuffleDeck()
        {
            int n = _deck.Count;
            while (n > 1)
            {
                int k = _rng.Next(n--);
                int temp = _deck[n];
                _deck[n] = _deck[k];
                _deck[k] = temp;
            }
        }

        // Public Methods Begin
        public static int Little_Joker => 0x50f; // 小王
        public static int Big_Joker => 0x610; // 大王


        public int RemainingCount => _deck.Count - _currentIndex;


        public int GetNextCode()
        {
            if (_currentIndex >= _deck.Count)
                return -1;

            return _deck[_currentIndex++];
        }


        public void ResetDeck()
        {
            _currentIndex = 0;
            _rng = new System.Random();
            ShuffleDeck();
        }

        // Public Methods End
    }
}
